﻿// ———————————————————————–
// <copyright file="ICNETSOAConstants.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// INETSOAConstants.cs - "Constants For ICNETSOA"
// Project: "IC.NETSOA"- "ICNETSERVICES "
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010
// Author:      Don McGarry The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.ComponentModel;

namespace ICNETServices
{
  /// <summary>
  /// Contains the constants used in various pats of IC.NET
  /// </summary>
  public class ICNETSOAConstants
  {
    /// <summary>
    /// Log4net logging object
    /// </summary>
    private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    /// <summary>
    /// The path used in posting a EDXL-DE to IC.NET
    /// </summary>
    public const string DEPostServiceNamespace = "http://icnet.mitre.org/PostDE";
    /// <summary>
    /// The base icnet global namespace for WCF services
    /// </summary>
    public const string ICNETNamespace = "http://icnet.mitre.org";
    public const string ICNETQualifiedName = "icnet";
    public const string ValueListService = "ValueListService.svc";
    private static SOAConstantsConfig config;

    #region Public Accessors

    /// <summary>
    /// Initializes static members of the ICNETSOAConstants class. 
    /// </summary>
    static ICNETSOAConstants()
    {
      LoadConstants();
    }

    private static string _ICNET_ROOT;

    public static string ICNET_ROOT
    {
      get
      {
        if (_ICNET_ROOT == null)
        {
          _ICNET_ROOT = System.Environment.GetEnvironmentVariable("ICNET_ROOT", EnvironmentVariableTarget.Machine);

          if (_ICNET_ROOT == null)
          {
            Log.Warn("Missing ICNET_ROOT environment variable, defaulting to C:\\ICNET");
            _ICNET_ROOT = @"C:\ICNET";
          }
        }

        return _ICNET_ROOT;
      }
    }

    /// <summary>
    /// Gets the base URI
    /// </summary>
    public static string ICNETbaseURI
    {
      get { return config.ICNETbaseURI; }
    }

    /// <summary>
    /// Gets the resource port.
    /// </summary>
    public static string ResourcePort
    {
        get { return config.ResourcePort; }
    }

    /// <summary>
    /// Gets whether periodic clearing of the cache is enabled
    /// </summary>
    public static bool CacheClearEnabled
    {
      get { return config.EnableCacheClear; }
    }

    /// <summary>
    /// Gets the duration to keep the cache around
    /// </summary>
    public static int CacheClearDurationInMinutes
    {
      get 
      {
        int duration;
        bool bCanParse = Int32.TryParse(config.CacheClearDurationInMinutes, out duration);

        if (bCanParse)
        {
          return duration;
        }
        else
        {
          return 720;
        }
      }
    }

    /// <summary>
    /// Gets the duration to keep the cache around
    /// </summary>
    public static int CancelMessageRetainInMinutes
    {
      get
      {
        int duration;
        bool bCanParse = Int32.TryParse(config.CancelMessageRetainInMinutes, out duration);

        if (bCanParse)
        {
          return duration;
        }
        else
        {
          return 1440;
        }
      }
    }


    /// <summary>
    /// Gets the duration to keep the cache around
    /// </summary>
    public static int KMLLinkRefreshInSeconds
    {
        get
        {
            int seconds;
            bool bCanParse = Int32.TryParse(config.KMLLinkRefreshInSeconds, out seconds);

            if (bCanParse)
            {
                return seconds;
            }
            else
            {
                return 60;
            }
        }
    }

    /// <summary>
    /// Gets the Federation Service URI
    /// </summary>
    public static string ICNETfedURI
    {
      get { return "urn:icnet:mitre:org:FedDE"; }
    }

    /// <summary>
    /// Gets the URI of the  datalink.
    /// </summary>
    public static string ICNETdatalinkURI
    {
      get { return ICNETbaseURI + "/DataService.svc/"; }
    }

    /// <summary>
    /// Gets the URI for the datalink to the HTML directory.
    /// </summary>
    public static string ICNETdatalinkToHistoryDEHTML
    {
      get { return ICNETdatalinkURI + "History/DEHTML"; }
    }

    /// <summary>
    /// Gets the URI for the datalink to the HTML directory.
    /// </summary>
    public static string ICNETdatalinkToHTML
    {
      get { return ICNETdatalinkURI + "HTML/"; }
    }

    /// <summary>
    /// Gets the URI for the datalink to the XML directory.
    /// </summary>
    public static string ICNETdatalinkToXML
    {
      get { return ICNETdatalinkURI + "XML/"; }
    }

    /// <summary>
    /// Gets the URI for the datalink to the MIME directory. 
    /// </summary>
    public static string ICNETdatalinkToMIME
    {
      get { return ICNETdatalinkURI + "MIME/"; }
    }

    /// <summary>
    /// Gets the URI for the datalink to the MIME directory. 
    /// </summary>
    public static string ICNETdatalinkToHistoryMIME
    {
      get { return ICNETdatalinkURI + "History/MIME"; }
    }

    /// <summary>
    /// Gets the ContentObject Keys. 
    /// </summary>
    public static string ICNETContentKeyValue
    {
      get { return "ContentObjectKeys"; }
    }

/// <summary>
/// Gets the Exception.
/// </summary>
    public static string ICNETExceptionValue
    {
      get { return "Exception"; }
    }

    /// <summary>
    /// Gets the namespace used for the Federation service. 
    /// </summary>
    public static string DEFedServiceNamespace
    {
      get { return "FedDE"; }
    }

    /// <summary>
    /// Gets the namespace used for the Polling Service.
    /// </summary>
    public static string PollingServiceNamespace
    {
      get { return ICNETbaseURI + "/PollingService"; }
    }

    /// <summary>
    /// Gets the namespace used for the DEProcessing service. 
    /// </summary>
    public static string DEProcessingServiceNamespace
    {
      get { return ICNETbaseURI + "/DEProcessingService"; }
    }

    /// <summary>
    /// Gets the namespace used for the Exposure Processing service.
    /// </summary>
    public static string ExposureProcessingServiceNamespace
    {
      get { return ICNETbaseURI + "/ExposureProcessingService"; }
    }

    /// <summary>
    /// Gets the namespace used for the Keyword routing Service.
    /// </summary>
    public static string KeywordRoutingServiceNamespace
    {
      get { return ICNETbaseURI + "/KeywordRoutingService"; }
    }

    /// <summary>
    /// Gets the namespace used for the Cap Processing Service.
    /// </summary>
    public static string CAPProcessingServiceNamespace
    {
      get { return ICNETbaseURI + "/CAPProcessingService"; }
    }

    /// <summary>
    /// Gets the namespace used for the REST Processing service.
    /// </summary>
    public static string RESTProcessingServiceNamespace
    {
      get { return ICNETbaseURI + "/RESTProcessingService"; }
    }

    /// <summary>
    /// Gets the namespace used for the Exception Service.
    /// </summary>
    public static string ExceptionServiceNamespace
    {
      get { return ICNETbaseURI + "/ExceptionService"; }
    }

    /// <summary>
    /// Gets nothing, because it is depricated.
    /// </summary>
    public static string DepricatedGeoRSSNS
    {
      get { return "http://www.w3.org/2003/01/geo/wgs84_pos#"; }
    }

    /// <summary>
    /// Gets the connection string used to connect to the ICNET database.
    /// </summary>
    public static string ICNETConnectionString
    {
      get { return "Data Source=localhost;Initial Catalog=" + config.ICNETDB + ";Integrated Security=True"; }
    }

    /// <summary>
    /// Gets the connection string used to connect to the ValueList database.
    /// </summary>
    public static string ValueListsConnectionString
    {
      get { return "Data Source=localhost;Initial Catalog=" + config.ValueListDB + ";Integrated Security=True"; }
    }

    /// <summary>
    /// Gets the connection string used to connect to the RouterRules database.
    /// </summary>
    public static string RouterRulesConnectionString
    {
      get { return "Data Source=localhost;Initial Catalog=" + config.RouterRulesDB + ";Integrated Security=True"; }
    }

    public static string HistoryConnectionString
    {
      get { return "Data Source=localhost;Initial Catalog=EDXLHistory;Integrated Security=True"; }
    }

    public static string RSSConnectionString
    {
      get { return "Data Source=localhost;Initial Catalog=RSS;Integrated Security=True"; }
    }

    /// <summary>
    /// Gets the URI for ICNET services.
    /// </summary>
    public static string ICNETServicesURI
    {
      get { return ICNETbaseURI + "/" + ValueListService + "/Roles/SenderRole/InternalServices"; }
    }

    /// <summary>
    /// Gets the URI for ICNET content.
    /// </summary>
    public static string ICNETContentURI
    {
      get { return ICNETbaseURI + "/" + ValueListService + "/ContentKeywords/InternalServices"; }
    }

    /// <summary>
    /// Gets the URI for ICNET exceptions.
    /// </summary>
    public static string ICNETExceptionURI
    {
      get { return ICNETbaseURI + "/" + ValueListService + "/ContentKeywords/InternalServices/Exceptions"; }
    }

    /// <summary>
    /// Gets the ICNET install path.
    /// </summary>
    public static string ICNETInstallPath
    {
      get { return config.ICNETInstallPath; }
    }


    #endregion

    /// <summary>
    /// Initializes the constants.
    /// </summary>
    private static void LoadConstants()
    {
      XmlSerializer s = new XmlSerializer(typeof(SOAConstantsConfig));
      TextReader r = null;
     
      string fileName = ICNET_ROOT + @"\Config\ICNET_SOA_Constants.xml";

      if (File.Exists(fileName))
      {
        r = new StreamReader(fileName);
      }
      else
      {
        Log.ErrorFormat("No config file: {0} Make sure the ICNET_ROOT system environment variable is correct.", fileName);
        return;
      }

      try
      {
        config = (SOAConstantsConfig)s.Deserialize(r);
        Log.InfoFormat("Deserialized configs from {0}.", fileName);
      }
      catch (Exception e)
      {
        Log.Error("Config file unparsable: " + fileName, e);
        config = null;
      }
      finally
      {
        r.Close();
      }
    }
  }

  /// <summary>
  /// Configuration for the Constants located in ICNETSOA.
  /// </summary>
  [Serializable]
  public class SOAConstantsConfig
  {
    /// <summary>
    /// Gets or sets the ICNET base URI.
    /// </summary>
    [XmlElement("ICNETbaseURI")]
    public string ICNETbaseURI
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the ICNET Resource Port.
    /// </summary>
    [XmlElement("ResourcePort")]
    public string ResourcePort
    {
        get;
        set;
    }

    /// <summary>
    /// Gets or sets the ICNET install path.
    /// </summary>
    [XmlElement("ICNETInstallPath")]
    public string ICNETInstallPath
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the string used to connect to the ICNET database. 
    /// </summary>
    [XmlElement("ICNETDB")]
    public string ICNETDB
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the string used to connect to the ValueList database. 
    /// </summary>
    [XmlElement("ValueListDB")]
    public string ValueListDB
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the string used to connect to the RouteRules database. 
    /// </summary>
    [XmlElement("RouterRulesDB")]
    public string RouterRulesDB
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or set whether to perform the periodic clearing of stale items from the cache
    /// </summary>
    [XmlElement("EnableCacheClear")]
    public bool EnableCacheClear
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the length of time in minutes to keep the cache around when the clear
    /// function is enabled
    /// </summary>
    [XmlElement("CacheClearDurationInMinutes", IsNullable=true)]
    public string CacheClearDurationInMinutes
    {
      get;
      set;
    }

      /// <summary>
      /// Gets or sets the length of time in minutes to retain cancel messages
      /// </summary>
    [XmlElement("CancelMessageRetainInMinutes",IsNullable=true)]
    public string CancelMessageRetainInMinutes
    {
        get;
        set;
    }

    /// <summary>
    /// Gets or sets the length of time in minutes to retain cancel messages
    /// </summary>
    [XmlElement("KMLLinkRefreshInSeconds", IsNullable = true)]
    public string KMLLinkRefreshInSeconds
    {
        get;
        set;
    }

  }
}
